package com.kfm.servlet;

import com.wf.captcha.utils.CaptchaUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/captcha")
public class CaptchaServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取验证码
        CaptchaUtil.out(200, 50, 4, req, resp);

//        System.out.println(req.getSession().getAttribute("captcha"));

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 校验验证码
        String captcha = req.getParameter("captcha");

        Object text = req.getSession().getAttribute("captcha");

        resp.setContentType("text/html;charset=utf-8");
        if (text.equals(captcha)){
            resp.getWriter().write("验证码输入正确");
        }else {
            resp.getWriter().write("验证码输入错误");
        }

//        CaptchaUtil.ver(captcha, req);
    }
}
